﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.OleDb;
using System.Data;
using DTO;

namespace DAO
{
    public class ChucVuDAO
    {
        public static List<ChucVuDTO> LayDanhSach()
        {
            List<ChucVuDTO> lstChucVu = new List<ChucVuDTO>();
            OleDbConnection cnn = BoKetNoiCSDL.TaoKetNoi();
            string query = "select * from Chuc_Vu";
            OleDbCommand cmd = new OleDbCommand(query, cnn);
            cnn.Open();
            try
            {
                OleDbDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    ChucVuDTO ChucVuDto = new ChucVuDTO();
                    ChucVuDto.MaChucVu = (int)dr["ChucVuID"];
                    ChucVuDto.TenChucVu = (string)dr["TenChucVu"];
                    lstChucVu.Add(ChucVuDto);
                }
            }
            catch
            {
                if (cnn.State == ConnectionState.Open)
                    cnn.Close();
                throw new Exception("thao tác lấy danh sách chức vụ bị lỗi");
            }
            return lstChucVu;
        }

        public static int Them(ChucVuDTO ChucVuDto)
        {
            OleDbConnection cnn = BoKetNoiCSDL.TaoKetNoi();
            string query = string.Format("insert into Chuc_Vu(TenChucVu) values ('{0}')", ChucVuDto.TenChucVu);
            OleDbCommand cmd = new OleDbCommand(query, cnn);
            cnn.Open();
            int rs = 0;
            try
            {
                rs = cmd.ExecuteNonQuery();
                cnn.Close();
            }
            catch
            {
                if (cnn.State == ConnectionState.Open)
                    cnn.Close();
                throw new Exception("thao tác thêm chức vụ bị lỗi");
            }
            return rs;
        }
    }
}
